﻿using System;
using ChiWalker.Dynamics;
using ChiWalker.Helpers;
using ChiWalker.Settings;
using ChiWalker.Spell;
using ChiWalker.Utilities;
using Styx;
using Styx.Common.Helpers;
using Styx.TreeSharp;

namespace ChiWalker.Routines.Warrior
{
    internal class Protection
    {
        private static readonly WaitTimer GapCloser = new WaitTimer(TimeSpan.FromSeconds(3));
        private static readonly WaitTimer InteruptTimer = new WaitTimer(TimeSpan.FromSeconds(3));

        [Behavior(BehaviorType.Pull | BehaviorType.Combat)]
        [Spec(WoWSpec.WarriorProtection)]
        [Class(WoWClass.Warrior)]
        public static Composite WarriorProtection()
        {
            return
                new PrioritySelector(
                    Safers.EnsureTarget(),
                    Movement.MoveToLoS(ret => StyxWoW.Me.CurrentTarget),
                    Movement.FaceUnit(ret => StyxWoW.Me.CurrentTarget),
                    new NoGCDSpell(Spell.BerserkerRage, ret => StyxWoW.Me, "Going Berserk!",
                        ret => ChiWalkerSettings.Instance.Warrior.UseBerserkOnCd),
                    new NoGCDSpell(Spell.ShieldBarrier, ret => StyxWoW.Me,
                        ret =>
                            StyxWoW.Me.HealthPercent <= ChiWalkerSettings.Instance.Warrior.ShieldBarrierPct &&
                            StyxWoW.Me.CurrentTarget.IsCasting, true),
                    new NoGCDSpell(Spell.ShieldBlock, ret => StyxWoW.Me,
                        ret => StyxWoW.Me.GetAuraTimeLeft("Shield Block", true).TotalSeconds <= 1, true),
                    new NoGCDSpell(Spell.SpellReflection, ret => StyxWoW.Me.CurrentTarget,
                        ret =>
                            StyxWoW.Me.CurrentTarget.IsTargetingMeOrPet && StyxWoW.Me.CurrentTarget.IsCasting &&
                            InteruptTimer.IsFinished, ret => InteruptTimer.Reset()),
                    new CastOnSelf(Spell.ShieldWall,
                        ret => StyxWoW.Me.HealthPercent <= ChiWalkerSettings.Instance.Warrior.ShieldWallPct),
                    new CastOnSelf(Spell.LastStand,
                        ret => StyxWoW.Me.HealthPercent <= ChiWalkerSettings.Instance.Warrior.ShieldWallPct),
                    new CastOnMyTarget(Spell.HeroicThrow, ret => StyxWoW.Me.CurrentTarget.Distance >= 10),
                    new CastOnMyTarget(Spell.Charge,
                        ret => !ChiWalkerSettings.Instance.DisableMovement &&
                               GapCloser.IsFinished && StyxWoW.Me.CurrentTarget.Distance >= 9 &&
                               StyxWoW.Me.CurrentTarget.Distance <= 20, ret => GapCloser.Reset()),
                    new CastOnMyTarget(Spell.HeroicLeap,
                        ret => !ChiWalkerSettings.Instance.DisableMovement &&
                               GapCloser.IsFinished && StyxWoW.Me.CurrentTarget.Distance >= 9 &&
                               StyxWoW.Me.CurrentTarget.Distance >= 40, ret => GapCloser.Reset()),
                    Movement.MoveToUnit(ret => StyxWoW.Me.CurrentTarget),
                    new CastOnMyTarget(Spell.Pummel, ret => StyxWoW.Me.CurrentTarget.CanInterruptCurrentSpellCast &&
                                                            StyxWoW.Me.CurrentTarget.IsCasting &&
                                                            InteruptTimer.IsFinished,
                        ret => InteruptTimer.Reset()),
                    new CastOnMyTarget(Spell.VictoryRush,
                        ret =>
                            StyxWoW.Me.HealthPercent <= ChiWalkerSettings.Instance.Warrior.VictoryRushHp,
                        null, true),
                    new CastOnSelf(Spell.EnragedRegeneration,
                        ret =>
                            StyxWoW.Me.HealthPercent <= ChiWalkerSettings.Instance.Warrior.EnragedRegenerationHp),
                    new Decorator(ret => Clusters.GetClusterCount(StyxWoW.Me, Clusters.NearbyUnitsInCombatWithMe,
                        ClusterType.Radius, 9f) >= 2,
                        new PrioritySelector(
                            new CastOnMyTarget(Spell.ThunderClap),
                            new CastOnMyTarget(Spell.Bladestorm)
                            )),
                    new CastOnMyTarget(Spell.Revenge),
                    new Decorator(ret => Clusters.GetClusterCount(StyxWoW.Me, Clusters.NearbyUnitsInCombatWithMe,
                        ClusterType.Cone, 10f) >= 2,
                        new PrioritySelector(
                            new NoGCDSpell(Spell.Cleave, ret => StyxWoW.Me.CurrentTarget,
                                ret =>
                                    StyxWoW.Me.RagePercent >= 60 && StyxWoW.Me.HealthPercent >= 80,
                                true),
                            new CastOnMyTarget(Spell.Shockwave),
                            new CastOnMyTarget(Spell.DragonRoar)
                            )),
                    new NoGCDSpell(Spell.HeroicStrike, ret => StyxWoW.Me.CurrentTarget,
                        ret =>
                            StyxWoW.Me.ActiveAuras.ContainsKey("Ultimatum"),
                        true),
                    new CastOnSelf(Spell.Bloodbath),
                    new CastOnMyTarget(Spell.StormBolt),
                    new CastOnSelf(Spell.Avatar,
                        ret => SettingsAnalyizer.AnalizeSpellUsage(ChiWalkerSettings.Instance.Warrior.Avatar)),
                    new CastOnMyTarget(Spell.ShieldSlam),
                    new CastOnSelf(Spell.CommandingShout),
                    new CastOnMyTarget(Spell.SunderArmor)
                    );
        }
    }
}